import jsPDF from 'jspdf';
import html2canvas from 'html2canvas';

export const generatePDF = async (content, fileName) => {
    const template = content;
    document.body.appendChild(template);
    await html2canvas(template, {
        scale: 1,
        logging: true,
        useCORS: true,
        async: true,
        allowTaint: false,
        width: 210 * 4,
    }).then(canvas => {
        document.body.removeChild(template);
        const imgData = canvas.toDataURL('image/png');
        const pdf = new jsPDF('p', 'mm', 'a4');
        const imgWidth = 210;
        const pageHeight = 297;
        const imgHeight = (canvas.height * imgWidth) / canvas.width;
        let heightLeft = imgHeight;
        let position = 0;
        pdf.addImage(imgData, 'PNG', 0, position, imgWidth, imgHeight);
        heightLeft -= pageHeight;
        while (heightLeft >= 0) {
            position = heightLeft - imgHeight;
            pdf.addPage();
            pdf.addImage(imgData, 'PNG', 0, position, imgWidth, imgHeight);
            heightLeft -= pageHeight;
        }
        pdf.save(`${fileName}.pdf`);
    });
}
export default generatePDF;